<?php

namespace app\app\controller;

use app\app\model\ProjectIntoModel;
use app\app\model\ProjectModel;
use app\app\model\ProjectPaperFileModel;
use app\BaseController;

class Analysis extends BaseController
{
    public function analysis()
    {

        $project_name = input('project_name', '');
        $where = "access =1";
        $start_time = date('Y-m-d 00:00:00', time());
        $end_time = date('Y-m-d 23:59:59', time());
        if ($project_name) $where .= " AND a.project_name  LIKE '%" . $project_name . "%'";
        $list = ProjectModel::field('id,project_name')->where($where)->select()->toArray();
        $project_total = count($list);

        $jc_toral = 0;
        foreach ($list as $key => $value) {
            if ($value['id'] == '30f8164a-011e-481d-bada-884a3a7ebfb7') {
                $list[$key]['total'] = 1706;
            } else {
                $list[$key]['total'] = ProjectIntoModel::
                where('type', '在场')
                    ->where('project_id', $value['id'])
                    ->where('create_time', 'between', [$start_time, $end_time])->count('id');
            }
            $jc_toral = $jc_toral + $list[$key]['total'];
        }

//        $jc_toral = 0;
//        if ($project_total) {
//            $jc_toral = array_sum(array_column($list, 'total'));
//        }

        $tongJi = array(
            'presence_toatal' => 4805,
            'rk_total' => 56,
            'unusual_total' => 2,
            'fix_total' => $project_total,
            'project_total' => $project_total,
            'jc_toral' => $jc_toral
        );
        $res = array(
            'total' => $tongJi,
            'list' => $list
        );

        $this->success('success', $res);
    }

    public function detail()
    {
        $project_id = input('project_id', '');
        $start_time = date('Y-m-d 00:00:00', time());
        $end_time = date('Y-m-d 23:59:59', time());
        $type = input('type', '在场');
        $where[] = ['into_time', 'between', [$start_time, $end_time]];
        if ($project_id) $where[] = ['project_id', '=', $project_id];
        if ($type) $where[] = ['type', '=', $type];
        $list = ProjectIntoModel::where($where)->order('into_time desc')->select();
        $this->success('success', $list);
    }


    public function info()
    {
        $where = " a.is_access =2";
        $list = ProjectModel::alias('a')->field('a.id,a.project_name')
            ->leftJoin('t_project_into b', 'a.id= b.project_id')
            ->where($where)->group('project_name')->select()->toArray();
        $this->success('success', $list);
    }


    public function projectInfo()
    {
        $type = input('type', 1);

        $year = date('Y', time());

        //按月筛选
        $start_time = $year . '-01-01 00:00:00';
        $end_time = date('Y-m-d 23:59:59', time());

        $where = "is_parent = 0";
        $kg_time_real_where = "kg_time_real BETWEEN '" . $start_time . "' AND '" . $end_time . "'";
        $kg_time_where = "kg_time LIKE '%" . $year . "%'";

        $tc_time_real_where = "tc_time >= '" . $start_time . "'  AND tc_time_real <= '" . $end_time . "'";
        $tc_time_where = "tc_time LIKE '%" . $year . "%'";
        $field = "project_name,kg_time,kg_time_real,tc_time,tc_time_real";
        switch ($type){
            case 1:
                $list = ProjectModel::field($field)->where($where)->where($tc_time_where)->select();
                break;
            case 2:
                $list = ProjectModel::field($field)->where($where)->where($tc_time_real_where)->select();
                break;
            case 3:
                $list =  ProjectModel::field($field)->where($where)->where($kg_time_where)->select();
                break;
            case 4:
                $list = ProjectModel::field($field)->where($where)->where($kg_time_real_where)->select();
                break;
            default:
                $list = [];
                break;
        }
        $this->success('success', $list);

    }



}
